var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var userName = sessionStorage.getItem("userName");
var fullname = sessionStorage.getItem("fullname");

$(function() {

	if(userId == "" || userId == undefined || userId == null) {
		//		alert("请登陆之后再操作");
		top.location.href = "login.html";
	} else {
		userId = userId;
		var pageNumber = "1";
		var pageSize = "13";

		//	var userId = "";
		var reque = function() {
			return {
				pageSize: pageSize,
				pageNumber: pageNumber,
			}
		}
		rq1 = reque();
		var req = function() {
			return {
				requestString: "",
				userId: userId,
				fullname: fullname
			}
		}
		requestObj1 = req();
		requestObj1.requestString = JSON.stringify(rq1);
		var req3 = JSON.stringify(requestObj1);
		console.log(req3)
		$.ajax({
			contentType: "application/json; charset=utf-8",
			type: "POST",
			url: urltest + 'verify/function/selectWodouUseRulesList',
			data: req3,
			async: false,
			dataType: "json",
			success: function(msg) {
				console.log(msg);
				if(msg.responseCode == 200) {

					$("#count").html(msg.count);
					var gezi = msg.count;
					if(gezi % 13 == 0) {
						var lenn = parseInt(gezi / 13);
						if(lenn == 0) {
							var lenn = 1;
						}
					} else {
						var lenn = parseInt(gezi / 13) + 1;
					}
					test("Test bootstrap v3 rendering", function() {
						var element = $('#bp-3-element-test');
						var options = {
							bootstrapMajorVersion: 3,
							currentPage: 1,
							numberOfPages: 5,
							totalPages: lenn
						}
						element.bootstrapPaginator(options);
						var element = $('#bp-3-element-test');
						ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
						ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
						var list = element.children();
						for(var i = 0; i < list.length; i++) {
							var item = $(list[i]);
							ok(item.is("li"), "Element " + i + " should be li");
						}
					});
					var table_msg = msg.returnString;
					createShowingTable(table_msg);
				} else {
					alert(msg.resoponseDisp)
				}
			},
			error: function() {
				alert("获取信息失败");
			}
		});
	}

});

$("#bp-3-element-test").on("click", function() {
	var aa = $(this).children(".active").children("a").html();
	var pageNumber = aa;
	var pageSize = "13";
	//	var userId = "";
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/function/selectWodouUseRulesList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				console.log(msg);
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 13 == 0) {
					var lenn = parseInt(gezi / 13);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 13) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: aa,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {
		console.log(bb);
		var bb = JSON.parse(bb);
		var tableStr = "";
		var len = bb.length;
		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			for(var i = 0; i < len; i++) {
				//				debugger
				if(bb[i].id == undefined) {
					bb[i].id = ""
				}
				if(bb[i].kindname == undefined) {
					bb[i].kindname = ""
				}
				switch(bb[i].kindname) {
					case 1:
						bb[i].kindname = "每日签到";
						break;
					case 2:
						bb[i].kindname = "邀请赠送";
						break;
					case 3:
						bb[i].kindname = "满额赠送";
						break;
						//					case 4:
						//						bb[i].kindname = "水电燃缴费";
						//						break;
				}
				if(bb[i].type == undefined) {
					bb[i].type = ""
				} else if(bb[i].type == "1") {
					bb[i].type = "固定额度"
				} else if(bb[i].type == "2") {
					bb[i].type = "金额比例"
				}
				if(bb[i].amount == undefined) {
					bb[i].amount = ""
				}
				if(bb[i].max == undefined) {
					bb[i].max = ""
				}
				if(bb[i].createusername == undefined) {
					bb[i].createusername = ""
				}
				if(bb[i].createdate == undefined) {
					bb[i].createdate = ""
				}

				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb[i].kindname + '</th>' +
					'<th>' + bb[i].id + '</th>' +
					'<th>' + bb[i].type + '</th>' +
					'<th>' + bb[i].amount + '</th>' +
					'<th>' + bb[i].max + '</th>' +
					'<th>' + bb[i].createusername + '</th>' +
					'<th>' + bb[i].createdate + '</th>' +
					//					'<th data-toggle="modal" data-target="#myModal" style="color:blue;cursor: pointer" ;><span class="glyphicon glyphicon-eye-open"></span></th>' +
					'<th data-toggle="modal" data-target="#myModal8" style="color:blue;cursor: pointer" ;><span class="glyphicon glyphicon-edit"></span></th>' +

					'</tr>';
			}
			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);
		}
		//点击修改
		$("#tbbb tr th:nth-child(9)").click(function() {
			//			debugger
			$("#wd_c_id").html($(this).siblings().eq(2).html());
			$("#wd_c_tj").html($(this).siblings().eq(1).html());
			$("#wd_c_lx").html($(this).siblings().eq(3).html());
			$("#wd_c_bl").val($(this).siblings().eq(4).html());
			$("#wd_c_fdmax").val($(this).siblings().eq(5).html());
			//			$("#wuye_shi_f").html($(this).siblings().eq(6).html());			
			$("#tebutt_1").click(function() {
				//必须
				debugger
				var id = $("#wd_c_id").html();
				var amount = $("#wd_c_bl").val();
				var max = $("#wd_c_fdmax").val();
				if(amount == "") {
					alert("金额或者比例尚未填写")
				} else {
					if(amount >= 1) {
						if(max != "") {
							alert("金额和最大值不能同时存在！请重新填写")
						} else {
							var reque = function() {
								return {
									id: id,
									amount: amount,
									max: max,
									modifyusername: userName,
									modifyuserid: userId
								}
							}
							rq1 = reque();
							var req = function() {
								return {
									requestString: "",
									userId: userId,
									fullname: fullname
								}
							}
							requestObj1 = req();
							requestObj1.requestString = JSON.stringify(rq1);
							var req3 = JSON.stringify(requestObj1);
							//	console.log(star_time+end_time+search_main)
							$.ajax({
								contentType: "application/json; charset=utf-8",
								type: "POST",
								url: urltest + 'verify/function/updateWodouUseRules',
								data: req3,
								async: true,
								dataType: "json",
								success: function(msg) {
									console.log(msg);
									if(msg.responseCode == 200) {
										alert("修改成功");
										location.href = "wodoushezhi.html";
									} else {
										alert(msg.resoponseDisp)
									}
								},
								error: function() {
									alert("获取信息失败");
								}
							});
						}
					} else {
						var reque = function() {
							return {
								id: id,
								amount: amount,
								max: max,
								modifyusername: userName,
								modifyuserid: userId
							}
						}
						rq1 = reque();
						var req = function() {
							return {
								requestString: "",
								userId: userId,
								fullname: fullname
							}
						}
						requestObj1 = req();
						requestObj1.requestString = JSON.stringify(rq1);
						var req3 = JSON.stringify(requestObj1);
						//	console.log(star_time+end_time+search_main)
						$.ajax({
							contentType: "application/json; charset=utf-8",
							type: "POST",
							url: urltest + 'verify/function/updateWodouUseRules',
							data: req3,
							async: true,
							dataType: "json",
							success: function(msg) {
								console.log(msg);
								if(msg.responseCode == 200) {
									alert("修改成功");
									location.href = "wodoushezhi.html";
								} else {
									alert(msg.resoponseDisp)
								}
							},
							error: function() {
								alert("获取信息失败");
							}
						});
					}
				}

			})

		});

		//		$("#Form1_f .file-drop-zone-title").html('<img src="dist/img/avatar2.png"/>')
		//删除
		$("#tebutt_2").click(function() {
			var id = $("#wd_c_id").html();
			var reque = function() {
				return {
					id: id,
				}
			}
			rq1 = reque();
			var req = function() {
				return {
					requestString: "",
					userId: userId,
					fullname: fullname
				}
			}
			requestObj1 = req();
			requestObj1.requestString = JSON.stringify(rq1);
			var req3 = JSON.stringify(requestObj1);

			$.ajax({
				contentType: "application/json; charset=utf-8",
				type: "POST",
				url: urltest + 'verify/function/deleteWodouUseRules',
				data: req3,
				async: true,
				dataType: "json",
				success: function(msg) {
					console.log(msg);
					if(msg.responseCode == 200) {
						console.log(msg);
						alert("删除成功！")
						location.href = "wodoushezhi.html";
					} else {
						alert(msg.resoponseDisp)
					}
				},
				error: function() {
					alert("获取信息失败");
				}
			});
			//			}
		})

	}
}
//类型改变
$("#wd_lx").change(function() {
	debugger
	var type = $("#wd_lx option:checked").val();
	if(type == "2") {
		$(".wd_te_li").show();
	} else {
		$(".wd_te_li").hide();
	}
})
//增加沃豆使用规则
$(".wd_add").click(function() {
	debugger
	var id = $("#wd_id").val();
	var kindname = $("#wd_tj").val();
	var type = $("#wd_lx option:checked").val();
	var amount = $("#wd_je_bl").val();
	var max = $("#wd_fdmax").val();
	if(id == "") {
		alert("请填写id");
	} else if(kindname == "") {
		alert("请填写途径");
	} else if(type == "0") {
		alert("请选择类型")
	} else if(type == "1") {
		if(amount == "") {
			alert("请输入金额")
			return false;
		} else {
			var regExp = /^[0-9]*[1-9][0-9]*$/;
			isok = regExp.test(amount);
			if(!isok) {
				alert("金额必须为正整数")
				return false;
			} else {
				var reque = function() {
					return {
						id: id,
						kindname: kindname,
						type: type,
						amount: amount,
						createusername: userName,
						createuser: userId,
						max: max,
					}
				}
				rq1 = reque();
				var req = function() {
					return {
						requestString: "",
						userId: userId,
						fullname: fullname
					}
				}
				requestObj1 = req();
				requestObj1.requestString = JSON.stringify(rq1);
				var req3 = JSON.stringify(requestObj1);
				$.ajax({
					contentType: "application/json; charset=utf-8",
					type: "POST",
					url: urltest + 'verify/function/addWodouUseRules',
					data: req3,
					async: true,
					dataType: "json",
					success: function(msg) {
						console.log(msg);
						if(msg.responseCode == 200) {
							alert("增加成功");
							location.href = "wodoushezhi.html";
						} else {
							alert(msg.resoponseDisp)
						}
					},
					error: function() {
						alert("获取信息失败");
					}
				});
			}
		}
	} else if(type == "2") {
		if(amount == "") {
			alert("请输入比例")
			return false;
		} else {
			var regExp = /^0\.([0-9]){1,4}$/;
			isok = regExp.test(amount);
			if(!isok) {
				alert("比例必须为大于0小于1的小数，且小数点后最多四位，如：0.0002")
				return false;
			} else {
				var reque = function() {
					return {
						id: id,
						kindname: kindname,
						type: type,
						amount: amount,
						createusername: userName,
						createuser: userId,
						max: max,
					}
				}
				rq1 = reque();
				var req = function() {
					return {
						requestString: "",
						userId: userId,
						fullname: fullname
					}
				}
				requestObj1 = req();
				requestObj1.requestString = JSON.stringify(rq1);
				var req3 = JSON.stringify(requestObj1);
				$.ajax({
					contentType: "application/json; charset=utf-8",
					type: "POST",
					url: urltest + 'verify/function/addWodouUseRules',
					data: req3,
					async: true,
					dataType: "json",
					success: function(msg) {
						console.log(msg);
						if(msg.responseCode == 200) {
							alert("增加成功");
							location.href = "wodoushezhi.html";
						} else {
							alert(msg.resoponseDisp)
						}
					},
					error: function() {
						alert("获取信息失败");
					}
				});
			}
		}
	}

})